<?php

namespace app\admin\controller;

use think\Controller;
use app\model\users as Usermodel;
use app\model\remaber as rem;
use think\Request;
use think\captcha;
use think\Session;
use Redis;
use app\model\config as ConfigModel;

/**
 * 登录控制器
 *
 * @author Sunxiaodong
 */
class Login extends Controller {

    public function index() {

        //$rem = cookie("name");
        $intername = db("config")->where("id=1")->value("name"); //网站名称
        $title = "登录";
        $this->assign("title", $title);
        $list = ConfigModel::get(1)->toArray();
        $check = $list['is_check'];
        $this->assign("check", $check);
        $this->assign("intername", $intername);
//        $listss = db("auth")->order('auth_pid ASC,auth_id ASC')->select();
//        $a = $this->tree($listss);
//        var_dump($a);

        return $this->fetch();
    }

    //处理登录
    public function checklogin() {

        $data = input("post.");
        // $rem = input("rem");

        $username = $data['u_username'];
        $password = md5($data['u_password']);

        $list = ConfigModel::get(1)->toArray();
        $check = $list['is_check'];
        if ($check == 1) {
            $captcha = new captcha\Captcha();
            if (!$captcha->check(input("captcha"))) {
                return ["status" => 3, 'message' => "验证码填写错误！"];
            }
        }

        $rule = [
            'u_username|用户名' => 'require',
            'u_password|密码' => 'require|alphaNum',
        ]; //自定义规则
        $message = [
            'u_username.require' => '请填写用户名！',
            'u_password.require' => '请填写密码！',
            'u_password.alphaNum' => '密码格式不正确！',
        ];

        $res = $this->validate($data, $rule, $message);

        if (true !== $res) {

            return ["status" => -1, 'message' => "登录信息缺失且不正确！"];
        }

        if ($list = db("users")->where("u_username='$username' and u_password='$password'")->find()) {
            
            $status = $list['u_status'];
            if($status==2)
            {
                return ["status" => 2, 'message' => "用户已被冻结！"];
            }
            
            session("u_id", $list['u_id']); //用户ID
            session("u_username", $list['u_username']); //用户名
            session("u_rid", $list['u_rid']); //权限ID
            $request = Request::instance();
            $ip = $request->ip();

            if ($relist = db("remaber")->where("zhanghao='$username'")->find()) {
                //$ip = $relist['ip'];
                cookie('name', $username, 604800);
            } else {

                db("remaber")->where("zhanghao='$username'")->delete();
                $request = Request::instance();
                cookie('name', $username, 604800);
                $arr['zhanghao'] = $data['u_username'];
                $arr['mima'] = $data['u_password'];
                // $arr['ip'] = $request->ip();
                rem::create($arr); //本地一键登录 
            }
            return ["status" => 1, 'message' => "验证成功！"];
        } else {
            return ["status" => 2, 'message' => "用户名或密码错误！"];
        }
    }

    //本机一键登录
    public function quicklogin() {
        $request = Request::instance();
        $ip = $request->ip();
        $name = cookie('name');

        if ($name != "") {
            $lists = db("users")->where("u_username='$name'")->find();
            session("u_id", $lists['u_id']); //用户ID
            session("u_username", $lists['u_username']); //用户名
            session("u_rid", $lists['u_rid']); //权限ID
            return ["status" => 1, 'message' => "本地验证成功！"];
        } else {
            return ["status" => 2, 'message' => "本地验证失败,请尝试手动登录一次！"];
        }
    }

    //注销登录

    public function logout() {
        Session::clear();
        $this->redirect("/register");
    }

}
